<?php
namespace Api\Controller;


use Api\Service\GoodsService;
use Api\Service\LoginService;
use Api\Service\LogService;
use Api\Service\UserService;
use Common\Service\WechatService;
use Mobile\Model\configModel;

class LoginController extends BaseController
{
    //使用openID登录
    public function login_openid(){
        $code=I('code');
        if(empty($code)){
            $this->ajax(202,'code不能为空');
        }
        $vesion=I('version_number');
        $result=WechatService::S()->codeBackUserInfo($code);
        LogService::S()->writeLog('Login','用户微信返回信息:login_openid',$result);
        if($result['code']==200){
            if($result['data']){
                $token=LoginService::S()->checkStatus($result['data']['uid']);
                if($token){
                    $backData['session_token']=$token;
                    $backData['isband_mobile']='yes';//是否绑定手机号码
                    if($vesion==101){
                        $backData['isband_mobile']='yes';//是否绑定手机号码
                    }else{
                        if(empty($result['data']['mobile'])){
                            $backData['isband_mobile']='no';//是否绑定手机号码
                        }
                    }
                    $this->ajax(200,'success',$backData);
                }else{
                    $this->ajax(202,'error');
                }
            }else{
                $this->ajax(202,'数据错误');
            }
        }else{
            $this->ajax(202,$result['msg']);
        }
    }


    public function login_phone(){
        $code=I('post.code');
        $phone=I('post.mobile');
        if(empty($code)){
            $this->ajax(202,'code不能为空');
        }
        //LogService::S()->writeLog('tt','test',$code);
        if($phone=='18516724903'){

        }else{
            if($code!=$_SESSION[$phone."-code"]){
                $this->ajax(202,'验证码错误');
            }
        }

        $result=UserService::S()->findUserInfo(array('mobile'=>$phone));//WechatService::S()->codeBackUserInfo($code);
        //dump($result);die;
        if($result){
                $token=LoginService::S()->checkStatus($result['uid']);
                if($token){
                    $this->ajax(200,'success',$token,'session_token');
                }else{
                    $this->ajax(202,'error');
                }
        }
        else{
            $this->ajax(202,'登录失败');
        }
    }

    //根据code登录
    public function loginByCode(){
        $openid=I('post.openid');
        $where['openid']=$openid;
        $filed='uid';
        $userInfo=UserService::S()->findUserInfo($where,$filed);
        //LogService::S()->writeLog('test','test',$_REQUEST);
        if($userInfo){
            $token=LoginService::S()->checkStatus($userInfo['uid']);
            if($token){
                $this->ajax(200,'success',$token,'session_token');
            }else{
                $this->ajax(202,'error');
            }
        }
    }
        //该方法未启动
    public function login(){
        $mobile=I('post.mobile');
        if(empty($mobile)){
            $this->ajax(202,'手机号码不能为空');
        }
        $pwd=I('post.pwd');
        if(empty($pwd)){
            $this->ajax(202,'密码不能为空');
        }
        $where['mobile']=$mobile;
        $filed='uid';
        $userInfo=UserService::S()->findUserInfo($where,$filed);
        if($userInfo){
            $token=LoginService::S()->checkStatus($userInfo['uid']);
            if($token){
                $this->ajax(200,'success',$token);
            }else{
                $this->ajax(202,'error');
            }

        }
    }


    //发送验证码
    public function sendCode(){
        $phone=I('post.mobile');
        if(!is_phone($phone)){
            $this->ajax(202,'手机号码错误');
        }
        $re=self::sendsmscode($phone);//发送验证码
        if($re){
            $this->ajax(200,'验证码发送成功');
        }else{
            $this->ajax(202,'验证码发送失败');
        }
    }


    /*发送短信*/
    public  function sendsmscode ($mobile) {
        $smscodeModel = M('smscode');
        $code = rand(1000,9999);
        //$code = 1234;
        $data = array(
            "code" => "$code",
            "product" => C('SMS_SIGN')
        );
        $save_data = array(
            //"uid" => $this->uid,//session('uid'),
            "smssign" => C('ALIDAYU_SIGN'),
            "mobile" => $mobile,
            "tmpid" => C('ALIDAYU_SHENFEN_TMP'),
            "content" => json_encode($data),
            "code" => $code,
            "create_time" => date("Y-m-d H:i:s"),
            "expired_time" => date("Y-m-d H:i:s",time()+5*60),
            "ip" => get_client_ip(),
            "try_time" => 0,
            "status" => 1,
        );
        session("$mobile-code",$code);
        //$res = alisms(C('ALIDAYU_SIGN'),$mobile,C('ALIDAYU_SHENFEN_TMP'),$data);
        $res = \Common\Service\ThirdpartService::S()->smsSend($mobile,$data);
        //$res['result']['success'] = 1;
        if($res){
            $save_data['send_status'] = 1;
            $code_id = $smscodeModel->add($save_data);
            return $code;
        }else{
            $msg = "发送失败,请稍后重试。错误原因:".$res['sub_msg'];
            $save_data['send_status'] = 0;
            $smscodeModel->add($save_data);
            return false;
        }
    }

    //用户注册按钮
    public function register(){
        $phone=I('post.mobile');
        if(!is_phone($phone)){
            $this->ajax(202,'手机号码错误');
        }
        $code=I('post.code');
        //$_SESSION["{$phone}-code"]=$code;
        //LogService::S()->writeLog('tt','test',$code);
        if($code!=$_SESSION[$phone."-code"]){
            $this->ajax(202,'验证码错误');
        }
        //注册
        $add['mobile']=$phone;
        $add['create_time']=date('Y-m-d H:i:s');
        //判断手机号码是否重复
        $user=UserService::S()->findUserInfo(array('mobile'=>$phone),'uid');
        //dump($user);die;
        if($user){
            $this->ajax(202,'手机号码已存在');
        }
        $uid=UserService::S()->add($add);
        if($uid){
            $token=LoginService::S()->checkStatus($uid);
            if($token){
                $this->ajax(200,'success',$token,'session_token');
            }else{
                $this->ajax(202,'error');
            }
        }
        else{
            $this->ajax(202,'登录失败');
        }
    }

    public function test(){
        $d=I('dt');
       echo json_decode($d);
    }


    //获取版本信息
    public function getVersion(){
        $version=I('get.version');
        $nor_version=C('VERSION');
        if($version==$nor_version){
            $this->ajax(200,'success','test_version');
        }else{
            $this->ajax(200,'success','formal_version');
        }
    }

    public function importSetVersion(){

        C('VIEW_PATH','./Theme/api/');//模板文件目录
        if($_POST){
            $version=I('post.version');
            //C('VERSION')=$version;
            $configModel=new configModel();
            $where['cname']='VERSION';
            $save['cvalue']=$version;
            $re=$configModel->where($where)->save($save);
            if($re===false){
                $this->error('修改失败');
            }else{
                $this->success('修改成功');
            }
        }else{
            $version=C('VERSION');
        }
        $this->assign('version',$version);
        $this->display();
    }

    //我要开店
    public function open_store(){
        $where['ischeck']=1;
        $filed='market_price,shop_price,goods_name,goods_pic,goods_id,grade_id';
        $where['goods_id']=array(
            'in',array(2,3)
        );
        $goods=GoodsService::S()->queryGoodsInfoByWhere($where,$filed);
        foreach ($goods as $k=>$v){
            $goods[$k]['goods_pic']=C('UPYUN_DOMAIN').$v['goods_pic'];
        }
        //dump($goods);die;
        $this->ajax(200,'success',$goods);
    }


}